import {Component, OnInit} from '@angular/core';
import {TipService} from "../../../services/tip.service";
import {ExtendHttpService} from "../../../../common/extend-http.service";
import {Router} from "@angular/router";
import Common from '../../../../common/common';

@Component({
  selector: 'app-reserve-submit',
  templateUrl: './reserve-submit.component.html',
  styleUrls: ['./reserve-submit.component.css']
})
export class ReserveSubmitComponent implements OnInit {

  submitData = {
    'book.name': '',
    'book.mobile': '',
    'book.start_time': '',
    'start_date': '',
    'start_time': '',
    'book.people_num': '',
    'book.content': '',
    'book.tid': ''
  };

  isLoading = false;

  tipType = 'error';

  submitType = true;

  tableList = [];

  constructor(private tipService: TipService,
              private extendHttp: ExtendHttpService,
              private router: Router) {
  }

  ngOnInit() {
    cBack = () => {
      $('#btn-back').trigger('click');
    }
    this.getTableList();
  }

  onSubmit() {
    if (!this.isLoading) {
      if (!this.submitData['book.name'].trim()) {
        this.tipType = 'error';
        this.tipService.change(true, '请输入预约姓名');
        return;
      }

      if (!this.submitData['book.mobile'].trim()) {
        this.tipType = 'error';
        this.tipService.change(true, '请输入预约手机号');
        return;
      }

      if (!Common.mobileReg(this.submitData['book.mobile'])) {
        this.tipType = 'error';
        this.tipService.change(true, '手机号格式不正确');
        return;
      }

      if (!this.submitData['start_date'].trim()) {
        this.tipType = 'error';
        this.tipService.change(true, '请选择预约日期');
        return;
      }
      if (!this.submitData['start_time'].trim()) {
        this.tipType = 'error';
        this.tipService.change(true, '请选择预约时间');
        return;
      }

      this.submitData['book.start_time'] = this.submitData['start_date'] + ' ' + this.submitData['start_time'] + ':00';
      this.isLoading = !this.isLoading;
      this.extendHttp.post('/merchantApp/singleprocess/addBook', this.submitData).subscribe(response => {
        let data = response.json();
        if (data.code == '0') {
          this.submitType = false;
          this.isLoading = !this.isLoading;
          this.tipType = 'success';
          this.tipService.change(true, '预定成功');

          setTimeout(() => {
            this.router.navigate(['/main/table/table-list']);
          }, 1500);
        } else if (data.code == '-2001') {
          this.tipType = 'error';
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.tipType = 'error';
          this.tipService.change(true, data.message);
          this.isLoading = !this.isLoading;
        }
      }, error => {
        this.tipType = 'error';
        this.tipService.change(true, '系统繁忙，操作失败！');
        this.isLoading = !this.isLoading;
      });
    }
  }

  getTableList() {
    this.extendHttp.post('/merchantApp/singleprocess/getAlltableList', {}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.tableList = data.list;
      } else if (data.code == '-2001') {
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

}
